package com.sihan.framework.etm.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * 日期处理工具类
 * @author gsl
 * 2017-7-4
 */
@Slf4j
public class DateUtils {
	/**
	 * yyyy-MM-dd hh:mm:ss
	 */
	public final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	/**
	 * yyyy-MM
	 */
	public final SimpleDateFormat SDF_Y_M = new SimpleDateFormat("yyyy-MM");
	/**
	 * yyyyMM
	 */
	public final SimpleDateFormat SDF_YM = new SimpleDateFormat("yyyyMM");
	/**
	 * yyyyMMdd
	 */
	public final SimpleDateFormat SDF_YM_DD = new SimpleDateFormat("yyyyMMdd");
	/**
	 * yyyy-MM-dd
	 */
	public final SimpleDateFormat SDF_YM_DD2 = new SimpleDateFormat("yyyy-MM-dd");
	public final SimpleDateFormat SDF_YM_DD3 = new SimpleDateFormat("yyyyMMdd");

	public final SimpleDateFormat SDF_YM_DD4 = new SimpleDateFormat("yyMMdd");
	/**
	 * yyyy
	 */
	public final SimpleDateFormat SDF_Y = new SimpleDateFormat("yyyy");
	/**
	 * MM
	 */
	public final SimpleDateFormat SDF_M = new SimpleDateFormat("MM");
	/**
	 * yyyy年MM月
	 */
	public final SimpleDateFormat SDF_Y_Y_M = new SimpleDateFormat("yyyy年MM月");
	/**
	 * 8:30
	 */
	public final SimpleDateFormat H_S = new SimpleDateFormat("HH:mm");
	
	public final SimpleDateFormat SDF_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyyMMddHHmmss");

	public final SimpleDateFormat SDF_HH_MM_SS = new SimpleDateFormat("HHmmss");
	
	/**
	 * yyMM
	 */
	public final SimpleDateFormat SDF_YYMM = new SimpleDateFormat("yyMM");
	
	public final SimpleDateFormat SDF_HH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static DateUtils dateUtils;
	
	private DateUtils() {}
	
	public static synchronized DateUtils getInstance() {
		if(null == dateUtils) {
			dateUtils = new DateUtils();
		}
		return dateUtils;
	}
	/**
	 * 获取当前时间
	 * 2017-7-4 17:13:00
	 */
	public String getCurrentDate(SimpleDateFormat dateFormat) {
		if(dateFormat == null) {
			return SDF.format(new Date());
		}
		return dateFormat.format(new Date());
	}
	/**
	 * 获取日期字符串
	 * 2017-7-4 17:13:00
	 */
	public String getDateStr(Date date,SimpleDateFormat dateFormat) {
		if(dateFormat == null) {
			return SDF.format(date);
		}
		return dateFormat.format(date);
	}
	
	public Date getDateByStr(String dateStr,SimpleDateFormat dateFormat) {
		Date date = new Date();
		try {
			date =  dateFormat.parse(dateStr);
		} catch (ParseException e) {
			log.error("格式化时间异常",e);
		}
		return date;
	}
	public String millisecondFormat(String ms) {
		if(StringUtils.isEmpty(ms) || !RegexpUtils.matched(ms, RegexpUtils.IS_NUM)) {
			return "";
		}
		long _ms = Long.parseLong(ms);
		Integer ss = 1000;  
	    Integer mi = ss * 60;  
	    Integer hh = mi * 60;  
	    Integer dd = hh * 24;  
	  
	    Long day = _ms / dd;  
	    Long hour = (_ms- day * dd) / hh;  
	    Long minute = (_ms - day * dd - hour * hh) / mi;  
	    Long second = (_ms - day * dd - hour * hh - minute * mi) / ss;  
	    Long milliSecond = _ms - day * dd - hour * hh - minute * mi - second * ss;  
	      
	    StringBuffer sb = new StringBuffer();  
	    if(day > 0) {  
	        sb.append(day+"天");  
	    }  
	    if(hour > 0) {  
	        sb.append(hour+"小时");  
	    }  
	    if(minute > 0) {  
	        sb.append(minute+"分");  
	    }  
	    if(second > 0) {  
	        sb.append(second+"秒");  
	    }  
	    if(milliSecond > 0) {  
//	        sb.append(milliSecond+"毫秒");  
	    }  
	    return sb.toString(); 
	}
	/**
	 * 时间加减 以月为单位
	 * 
	 * @param d
	 *            基础时间
	 * @param months
	 *            月份数， 为正则往后,为负则往前
	 * @return
	 */
	public Date getAddMonths(Date d, int months) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(d);
		gc.add(2, months);
		return gc.getTime();
	}
	/**
	 * 获取连续几年年份/月份字符串
	 * @param i
	 * @param sdfY
	 * @return
	 */
	public List<Object> getContinuityTime(int i, SimpleDateFormat sdfY) {
		List<Object> list = new ArrayList<Object>();
        Calendar c = Calendar.getInstance();
        for (int j = 0; j < i; j++) {
        	 c.add(Calendar.YEAR, j>0?-1:-j);
             c.set(Calendar.MONTH, Calendar.JANUARY);
             c.set(Calendar.DAY_OF_MONTH, 1);
             list.add(sdfY.format(c.getTime()));
		}
        return list;
	}
	/**
	 * 获取连续12月月份字符串
	 * @param i
	 */
	public List<Object> getContinuityMonth(int i) {
		List<Object> list = new ArrayList<Object>();
        Calendar c = Calendar.getInstance();
        for (int j = 0; j < i; j++) {
        	 c.add(Calendar.MONTH, j>0?-1:-j);
           
             list.add(SDF_Y_M.format(c.getTime()));
		}
        return list;
		
	}
	
	/**
	 * 
	 * @description  产生连续的年
	 * @param length
	 * @return
	 * @auther 韩兵
	 * @Date 2020年7月26日
	 * @return Integer[]
	 */
	public static Integer[] produceContinueYearBy(int length) {
		Integer[] years = new Integer[length];
		Calendar c = Calendar.getInstance();
		for(int i=0;i<length;i++) {
			years[length-i-1] = c.get(Calendar.YEAR);
			c.add(Calendar.YEAR, -1);
		}
		return years;
	}
	
	/**
	 * 
	 * @description  生成连续的月
	 * @param length
	 * @return
	 * @auther 韩兵
	 * @Date 2020年7月26日
	 * @return String[]
	 */
	public static String[] produceContinueMonth(int length) {
		String[] months = new String[length];
		Calendar c = Calendar.getInstance();
		for(int i=0;i<length;i++) {
			months[length-i-1] = (c.get(Calendar.MONTH)+1)+"月";
			c.add(Calendar.MONTH, -1);
		}
		return months;
	}
	
	/**
	 * 
	 * @description  返回当前时间  年月  2012
	 * @return
	 * @auther 韩兵
	 * @Date 2020年7月29日
	 * @return String
	 */
	public String produceTwoYearTwoMonth() {
		Calendar c = Calendar.getInstance();
		return SDF_YYMM.format(c.getTime());
	}

	public String formatTimestamp(Long timestamp,SimpleDateFormat format){
		//获取时间戳
		return format.format(timestamp);
	}
}
